﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using AcctruePMS.Bll.Common;
using AcctrueWMS.Foundation.Data;
using AcctrueWMS.Foundation.DAL;

namespace AcctruePMS.Bll.Manager
{
    public class SysDictManager
    {
        
        /// <summary>
        /// 得到一个词典的所有项
        /// </summary>
        /// <param name="dictName"></param>
        /// <returns></returns>
        public DataTable GetDictItems(string dictName)
        {
            return WMSqlExcuter.ExecuteDataTable("select dictKey as [key],dictValue as [value] from sys_dictionary_items where dictName=@dictName",
                CommandType.Text,
                DBFactory.NewParameter("dictName",dictName));
        }

        /// <summary>
        /// 得到一个词典的所有项,并按照键进行排序
        /// </summary>
        /// <param name="dictName"></param>
        /// <param name="orderType">排序方式</param>
        /// <returns></returns>
        public DataTable GetDictItems(string dictName,string orderType)
        {
            if(orderType.ToLower()!="asc" && orderType.ToLower()!="desc"){
              orderType="asc";
            }
            return WMSqlExcuter.ExecuteDataTable("select dictKey as [key],dictValue as [value] from sys_dictionary_items where dictName=@dictName order by dictKey "+orderType,
                CommandType.Text,
                DBFactory.NewParameter("dictName", dictName));
        }
        public string GetDictValue(string dictName, string key)
        {
            return DataChecker.CheckForNull(WMSqlExcuter.ExecuteScalar("select dictValue from sys_dictionary_items where dictName=@dictName and dictKey=@dictKey", 
                CommandType.Text,
                DBFactory.NewParameter("dictName",dictName),
                DBFactory.NewParameter("dictKey",key)));
        }
        public string GetDictText(string dictName, string key)
        {
            return DataChecker.CheckForNull(WMSqlExcuter.ExecuteScalar("select dictText from sys_dictionary_items where dictName=@dictName and dictKey=@dictKey",
                CommandType.Text,
                DBFactory.NewParameter("dictName", dictName),
                DBFactory.NewParameter("dictKey", key)));
        }
        /// <summary>
        /// 得到所有词典
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllDict()
        {
            return WMSqlExcuter.ExecuteDataTable(string.Format("select distinct dictName as [key],displayName as [value] from sys_dictionary"));
        }

        public string GetDictDisplayName(string dictName)
        {
            return DataChecker.CheckForNull(WMSqlExcuter.ExecuteScalar("select displayName from sys_dictionary where dictName=@dictName",
                CommandType.Text,
                DBFactory.NewParameter("dictName",dictName)));
        }


        /// <summary>
        ///根据活动类型获取对应的子活动
        /// </summary>
        /// <param name="key">活动类型键</param>
        /// <returns></returns>
        public DataTable GetChildActity(string key) {
            if (string.IsNullOrEmpty(key))
            {
                return null;
            }
            else {
                string sql = "select dictKey [key],dictValue [value] from dbo.sys_dictionary_items where dictName='childActity' and dictKey like '"+key+ "-%' ";

              return  WMSqlExcuter.ExecuteDataTable(sql);
            }

         
        }

        /// <summary>
        /// 获取一个键值对表
        /// </summary>
        /// <returns></returns>
        public DataTable GetKeyValue() {
            DataTable dt = new DataTable();
            DataColumn dc = new DataColumn("key", typeof(int));
            DataColumn dc1 = new DataColumn("value", typeof(string));
            dt.Columns.Add(dc);
            dt.Columns.Add(dc1);
            DataRow dr = dt.NewRow();
            dr["key"] = 0;
            dr["value"] = "未提交";
            dt.Rows.Add(dr);
            DataRow dr1 = dt.NewRow();
            dr1["key"] = 1;
            dr1["value"] = "已提交";
            dt.Rows.Add(dr1);
            return dt;
        }
    }
}
